<?php
/**
 * Created by PhpStorm.
 * UserTask: xuxiaoming
 * Date: 2017/11/7
 * Time: 17:55
 */

namespace App\Http\Controllers;


use App\Models\UserTask;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller;

class UserTaskController extends Controller
{
    /**
     * 列表
     * @return string
     */
    public function index()
    {
        $userTasks = UserTask::all();
        if ($userTasks) {
            return sendJsonData($userTasks->toArray());
        } else{
            return sendJsonMessage('数据不存在',true);
        }
    }


    /**
     * 列表
     * @param Request $request
     * @return string
     */
    public function getUserTask(Request $request, $id)
    {
        $userTask = UserTask::find($id);
        if ($userTask) {
            return sendJsonData($userTask->toArray());
        } else{
            return sendJsonMessage('数据不存在',true);
        }
    }

    /**
     * 创建用户
     * @param Request $request
     * @return string
     */
    public function createUserTask(Request $request)
    {
        $userTasks = UserTask::create($request->all());
        return sendJsonData($userTasks->toArray());
    }

    /**
     * 更新用户
     *
     * @param Request $request
     * @param $id
     * @return string
     */
    public function updateUserTask(Request $request, $id)
    {
        $userTasks = UserTask::find($id);
        $userTasks->user_id = $request->input('user_id');
        $userTasks->task_id = $request->input('task_id');
        $userTasks->save();
        return sendJsonData($userTasks->toArray());
    }

    /**
     * 删除用户
     *
     * @param $id
     * @return string
     */
    public function deleteUserTask($id)
    {
        $userTasks = UserTask::find($id);
        if ($userTasks) {
            $userTasks->delete();
            return sendJsonMessage('删除成功');
        } else{
            return sendJsonMessage('数据不存在',true);
        }

    }


}